package com.cloud.user.controller;

import com.cloud.mall.user.TbMember;
import com.cloud.user.service.ITbMemberService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器 GUN
 * </p>
 *
 * @author jobob
 * @since 2021-07-07
 */
@RestController
@RequestMapping("/user/tb-member")
public class TbMemberController {
    @Autowired
    private ITbMemberService iTbMemberService;

    @RequestMapping("/getById")
    public TbMember getById(@RequestParam("id") Long id) {
        if (null == id) {
            return null;
        }
        return iTbMemberService.getById(id);
    }

    @RequestMapping("/save")
    public boolean save(@RequestBody TbMember tbMember) {
        if (null == tbMember) {
            throw new RuntimeException("数据为空，插入失败");
        }
        return iTbMemberService.save(tbMember);
    }

    @RequestMapping("/delete")
    public boolean delete(@RequestParam("id") Long id) {
        if (null == id) {
          throw new RuntimeException("数据为空，删除失败");
        }
        return iTbMemberService.removeById(id);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody TbMember tbMember) {
        if (null == tbMember) {
           throw new RuntimeException("数据为空，修改失败");
        }
        return iTbMemberService.updateById(tbMember);
    }

    @RequestMapping("/listByPage")
    public PageInfo<TbMember> listByPage(@RequestBody TbMember tbMember) {
        int pageNum = 1;
        int pageSize = 10;
        if (null != tbMember && tbMember.getPageNum() != null) {
            pageNum = tbMember.getPageNum();
        }
        if (null != tbMember && tbMember.getPageSize() != null) {
           pageSize = tbMember.getPageSize();
        }
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<TbMember>(iTbMemberService.list());
    }

    @RequestMapping("/listAll")
    public List<TbMember> listAll() {
        return iTbMemberService.list();
    }
}
